setwd("~/data")
reg.data1 <- read.csv("SM_CalibratingAutonomy.csv")

## Check the univariate distribution of each variable before splitting it into terciles
{
  png(filename = "capacity.png", width = 8, height = 4, units = "in", res = 600)
  par(mar = c(5,12,3,2))
  barplot(table(reg.data1$Capacity), horiz = TRUE, 
          names.arg = c("I strongly disagree",
                        "I disagree",
                        "I do not agree or disagree",
                        "I agree",
                        "I strongly agree"),
          las = 1,
          xlab = "Frequency", 
          main = "My work unit is able to recruit people with the right skills."
  )
  dev.off()
}


{
  png(filename = "discretion.png", width = 8, height = 4, units = "in", res = 600)
  par(mar = c(5,12,3,2))
  barplot(table(reg.data1$Discretion), horiz = TRUE, 
          names.arg = c("Always",
                        "Often",
                        "Sometimes",
                        "Rarely",
                        "Never"),
          las = 1,
          xlab = "Frequency", 
          main = "How often do rules and formal regulations\nimpede your ability to do your job?"
  )
  dev.off()
}

{
  png(filename = "independence.png", width = 8, height = 4, units = "in", res = 600)
  par(mar = c(5,12,3,2))
  hist(reg.data1$Independence, main = "Independence index", xlab = "Independence")
  dev.off()
}
quantile(reg.data1$Independence, c(.333333, .666666))





### Diagnostic plots to check linear interaction assumptions

#install.packages("interflex") # Make sure that you install interflex
library(interflex)
reg.data2k2<- reg.data1 
reg.data2k2<- as.data.frame(as.matrix(sapply(reg.data2k2, as.numeric)))
set.seed(1)
reg.data2k2$Independence.jitter <- jitter(reg.data2k2$Independence)
reg.data2k2$Quality.jitter      <- jitter(reg.data2k2$Quality, amount = .08)
reg.data2k2$Discretion.jitter   <- jitter(reg.data2k2$Discretion, amount = .3)
reg.data2k2$Capacity.jitter     <- jitter(reg.data2k2$Capacity, amount = .3)

# Diagnostics: Checking the raw data with Linear Interaction Diagnostic (LID) plot for Independence
png(filename = "diagnostic_plot_independence_capacity.png", width = 9, height = 5, units = "in", res = 800)
interflex(estimator = "raw", 
          Y = "Quality.jitter", 
          D = "Independence.jitter", 
          X = "Capacity", 
          cutoffs = c(2.5, 3.5), 
          data = reg.data2k2, 
          weights = NULL, 
          ncols = 3, 
          Ylabel = "Quality", 
          Dlabel = "Independence", 
          Xlabel = "Capacity", 
          theme.bw = TRUE,
          show.grid = FALSE)
dev.off()

png(filename = "diagnostic_plot_discretion_capacity.png", width = 9, height = 5, units = "in", res = 800)
interflex(estimator = "raw", 
          Y = "Quality.jitter", 
          D = "Discretion.jitter", 
          X = "Capacity", 
          cutoffs = c(2.5, 3.5), 
          data = reg.data2k2, 
          weights = NULL, 
          ncols = 3, 
          Ylabel = "Quality", 
          Dlabel = "Discretion", 
          Xlabel = "Capacity", 
          theme.bw = TRUE,
          show.grid = FALSE)
dev.off()

png(filename = "diagnostic_plot_capacity_independence.png", width = 9, height = 5, units = "in", res = 800)
interflex(estimator = "raw", 
          Y = "Quality.jitter", 
          D = "Capacity.jitter", 
          X = "Independence", 
          data = reg.data2k2, 
          weights = NULL, 
          ncols = 3, 
          Ylabel = "Quality", 
          Xlabel = "Independence", 
          Dlabel = "Capacity", 
          theme.bw = TRUE,
          show.grid = FALSE)
dev.off()

png(filename = "diagnostic_plot_capacity_discretion.png", width = 9, height = 5, units = "in", res = 800)
interflex(estimator = "raw", 
          Y = "Quality.jitter", 
          D = "Capacity.jitter", 
          X = "Discretion", 
          cutoffs = c(2.5, 3.5),
          data = reg.data2k2, 
          weights = NULL, 
          ncols = 3, 
          Ylabel = "Quality", 
          Xlabel = "Discretion", 
          Dlabel = "Capacity", 
          theme.bw = TRUE,
          show.grid = FALSE)
dev.off()




